﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class FormDanhSachMonAn : Form
    {
        public FormDanhSachMonAn()
        {
            InitializeComponent();
            LoaiMonAnBUS lmaBUS = new LoaiMonAnBUS();
            List<LoaiMonAnDTO> listLoaiMonAn = lmaBUS.LayDanhSachLoaiMonAn();
            ccbLoaiMon.DataSource = listLoaiMonAn;
            ccbLoaiMon.DisplayMember = "Ten_Mon_An";
            ccbLoaiMon.ValueMember = "Id_Loai";

            LoaiMenuBUS lmnBUS = new LoaiMenuBUS();
            List<LoaiMenuDTO> listLoaiMenu = lmnBUS.LayDanhSachLoaiMenu();
            ccbLoaiMenu.DataSource = listLoaiMenu;
            ccbLoaiMenu.DisplayMember = "Ten_Loai_Menu";
            ccbLoaiMenu.ValueMember = "Id_Loai_Menu";

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormDanhSachMonAn_Load(object sender, EventArgs e)
        {
            List<MonAnDTO> danhsach = new List<MonAnDTO>();
            MonAnBUS bus = new MonAnBUS();
            danhsach = bus.DanhSachMonAn();
            gridViewXemDanhSachMonAn.Rows.Clear();
            for (int i = 0; i < danhsach.Count; i++)
            {
                gridViewXemDanhSachMonAn.Rows.Add(i+1,danhsach[i].Id_Mon_An, danhsach[i].Ten_Mon_An, danhsach[i].Id_Loai_Mon_An, danhsach[i].Id_Loai_Menu, danhsach[i].Don_Gia);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (chkChonTatCa.Checked)
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachMonAn.Rows)
                {
                    r.Cells[6].Value = true;
                }
            }
            else if (!(chkChonTatCa.Checked))
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachMonAn.Rows)
                {
                    r.Cells[6].Value = false;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Bạn Chắc Chắn Muốn Xóa Món Ăn Này?", "Xóa Món Ăn", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                List<DataGridViewRow> rowsToDelete = new List<DataGridViewRow>();
                foreach (DataGridViewRow row in gridViewXemDanhSachMonAn.Rows)
                {
                    DataGridViewCheckBoxCell chk = row.Cells[3] as DataGridViewCheckBoxCell;
                    if (Convert.ToBoolean(chk.Value) == true)
                        rowsToDelete.Add(row);
                }
                //loop through the list to delete rows added to list<T>:
                foreach (DataGridViewRow row in rowsToDelete)
                {
                    MonAnBUS tk = new MonAnBUS();
                    tk.XoaMonAn(Convert.ToInt32(row.Cells[1].Value));
                    gridViewXemDanhSachMonAn.Rows.Remove(row);
                }
            }
        }

        private void btnThemMonAn_Click(object sender, EventArgs e)
        {
            FormThemMonAn frm = new FormThemMonAn();
            frm.Show();
        }
        public static int ID;
        private void btnSua_Click(object sender, EventArgs e)
        {
            int chkDem = 0;
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachMonAn.Rows)
            {
                if (singleRow.Cells[6].Value != null)
                {
                    if ((Boolean)singleRow.Cells[6].Value == true)
                    {
                        chkDem = chkDem + 1;
                    }
                }
            }
            if (chkDem == 0 || chkDem > 1)
            {
                MessageBox.Show("Chỉ Được Chọn 1 Món Ăn Để Tiến Hành Sửa Đổi!");
                return;
            }
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachMonAn.Rows)
            {
                if (singleRow.Cells[6].Value != null)
                {
                    if ((Boolean)singleRow.Cells[6].Value == true)
                    {
                        ID = Convert.ToInt32(singleRow.Cells[1].Value.ToString());
                    }
                }
            }
            FormCapNhatMonAn f = new FormCapNhatMonAn();
            f.ShowDialog();
        }
        private void ccbLoaiMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<MonAnDTO> danhsach = new List<MonAnDTO>();
            MonAnBUS bus = new MonAnBUS();
            danhsach = bus.DanhSachMonAnTheoLoaiMonvaLoaiMenu(Convert.ToInt32(ccbLoaiMon.SelectedValue),Convert.ToInt32(ccbLoaiMenu.SelectedValue));
            gridViewXemDanhSachMonAn.Rows.Clear();
            for (int i = 0; i < danhsach.Count; i++)
            {
                gridViewXemDanhSachMonAn.Rows.Add(i + 1, danhsach[i].Id_Mon_An, danhsach[i].Ten_Mon_An, danhsach[i].Id_Loai_Mon_An, danhsach[i].Id_Loai_Menu, danhsach[i].Don_Gia);
            }
        }

        private void ccbLoaiMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<MonAnDTO> danhsach = new List<MonAnDTO>();
            MonAnBUS bus = new MonAnBUS();
            danhsach = bus.DanhSachMonAnTheoLoaiMonvaLoaiMenu(Convert.ToInt32(ccbLoaiMon.SelectedValue), Convert.ToInt32(ccbLoaiMenu.SelectedValue));
            gridViewXemDanhSachMonAn.Rows.Clear();
            for (int i = 0; i < danhsach.Count; i++)
            {
                gridViewXemDanhSachMonAn.Rows.Add(i + 1, danhsach[i].Id_Mon_An, danhsach[i].Ten_Mon_An, danhsach[i].Id_Loai_Mon_An, danhsach[i].Id_Loai_Menu, danhsach[i].Don_Gia);
            }
        }

        private void gridViewXemDanhSachMonAn_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
